<?php
    if(!isset($_POST['filter']))
    {
        $_POST['filter'] = '---';
    }
    if(file_exists('listOfExpenses.txt'))
    {
        $totalSum = 0;
        $counter = -1;
        //Extract and normalize the text from the file.
        $text = file_get_contents('listOfExpenses.txt');
        $text = trim($text);
        $allRecords = explode("\n", $text);
        foreach($allRecords as $record)
        {
            $counter++;
            $splitedRecord = explode("@", $record);

            if(($_POST['filter'] == '---' or $_POST['filter'] == 1) and (!isset($_POST['month']) or $_POST['month'] - 1 == '---') and (!isset($_POST['year']) or $_POST['year'] == '---'))
            {
                include('echoResults.php');
            }
            elseif ($_POST['filter'] == 1 and $_POST['month'] - 1 == '---' and $_POST['year'] == date('Y', $splitedRecord[0]))
            {
                include('echoResults.php');
            }
            elseif ($_POST['filter'] == 1 and $_POST['month'] - 1 == date('m', $splitedRecord[0]) and $_POST['year'] == '---')
            {
                include('echoResults.php');
            }
            elseif ($_POST['filter'] == 1 and $_POST['month'] - 1 == date('m', $splitedRecord[0]) and $_POST['year'] == date('Y', $splitedRecord[0]))
            {
                include('echoResults.php');
            }
            elseif($splitedRecord[3] == $_POST['filter'] and $_POST['month'] - 1 == '---' and $_POST['year'] == '---')
            {
                include('echoResults.php');
            }
            elseif($splitedRecord[3] == $_POST['filter'] and $_POST['month'] - 1 == date('m', $splitedRecord[0]) and $_POST['year'] == '---')
            {
                include('echoResults.php');
            }
            elseif($splitedRecord[3] == $_POST['filter'] and $_POST['month'] - 1 == '---' and $_POST['year'] == date('Y', $splitedRecord[0]))
            {
                include('echoResults.php');
            }
            elseif($splitedRecord[3] == $_POST['filter'] and $_POST['month'] - 1 == date('m', $splitedRecord[0]) and $_POST['year'] == date('Y', $splitedRecord[0]))
            {
                include('echoResults.php');
            }
        }
            echo '<tr>';
            echo '<td>-------</td>';
            echo '<td>-------</td>';
            echo '<td>'.$totalSum.'</td>';
            echo '<td>-------</td>';
            echo '</tr>';
    }
    else
    {
            echo '<tr>';
            echo '<td>-------</td>';
            echo '<td>-------</td>';
            echo '<td>---0---</td>';
            echo '<td>-------</td>';
            echo '</tr>';
    }
?>